﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LiveCharts;
using LiveCharts.Configurations;
using PkwDevFramework.Common;
using PkwDevFramework.WpfMvvm;
using PkwHCFramework.Entity.DomainModels;
using PkwHCFramework.Models;
using PkwHCFramework.ResourceControls.Controls;

namespace PkwHCFramework.ViewModels.Pages
{
    public class HomeViewModel : PageViewModel
    {
        private List<StatusTimeItem> _statusTimes;
        private ChartValues<DataModel> _outputValues;
        private ChartValues<DataModel> _energyValues;
        private List<AlarmModel> _alarms;

        private Repository<Alarm> _alarmRepository;

        public List<StatusTimeItem> StatusTimes
        {
            get { return _statusTimes; }
            set { SetProperty(ref _statusTimes, value); }
        }

        public ChartValues<DataModel> OutputValues
        {
            get { return _outputValues; }
            set { SetProperty(ref _outputValues, value); }
        }

        //能耗图表数据
        public ChartValues<DataModel> EnergyValues
        {
            get { return _energyValues; }
            set { SetProperty(ref _energyValues, value); }
        }

        public List<AlarmModel> Alarms
        {
            get { return _alarms; }
            set { SetProperty(ref _alarms, value); }
        }

        public HomeViewModel(Repository<Alarm> alarmRepository)
        {
            var config = Mappers
                .Xy<DataModel>()
                .X(model => (double)model.DataX)
                .Y(model => model.Value);
            Charting.For<DataModel>(config);
            _alarmRepository = alarmRepository;
            Initialize();
        }

        void Initialize()
        {
            var items = new List<StatusTimeItem>();
            items.Add(new StatusTimeItem() { Status = "Offline", Time = DateTime.Today });
            items.Add(
                new StatusTimeItem()
                {
                    Status = "Wait",
                    Time = DateTime.Today.AddHours(1).AddMinutes(1)
                }
            );
            items.Add(new StatusTimeItem() { Status = "Run", Time = DateTime.Today.AddHours(2) });
            items.Add(new StatusTimeItem() { Status = "Run", Time = DateTime.Today.AddHours(2) });
            items.Add(new StatusTimeItem() { Status = "Wait", Time = DateTime.Today.AddHours(3) });
            items.Add(new StatusTimeItem() { Status = "Failt", Time = DateTime.Today.AddHours(4) });
            items.Add(
                new StatusTimeItem()
                {
                    Status = "Run",
                    Time = DateTime.Today.AddHours(5).AddMinutes(29)
                }
            );
            StatusTimes = items;

            OutputValues = new ChartValues<DataModel>();
            //产量数据，生成12个月的产量模拟数据
            for (int i = 0; i < 12; i++)
            {
                var value = new DataModel();
                value.DataX = i + 1;
                value.Value = new Random().Next(500, 1000);
                OutputValues.Add(value);
            }

            EnergyValues = new ChartValues<DataModel>();
            for (int i = 0; i < 12; i++)
            {
                var value = new DataModel();
                value.DataX = i + 1;
                value.Value = new Random().Next(1000, 2000);
                EnergyValues.Add(value);
            }

            Alarms = new List<AlarmModel>();
        }

        public override void PageActiving(object sender, EventArgs e)
        {
            base.PageActiving(sender, e);
        }

        public override void PageInactived(object sender, EventArgs e)
        {
            base.PageInactived(sender, e);
        }
    }
}
